package com.gst.handfood.utils;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by YBH on 2017/10/17.
 * AppManager设计成单例模式
 *  统一app程序当中所有的activity栈管理
 *  添加、删除指定、删除当前、删除所有、求栈大小....
 */

public class AppManger {
    Stack<Activity> activityStack = new Stack<>();

    public static AppManger appManger = null;

    private AppManger() {

    }
    public static AppManger getInstance(){
        if (appManger==null) {
            appManger = new AppManger();
        }
        return appManger;
    }

    /**
     * 添加一个activity到栈里面去
     * @param activity
     */
    public void addActivity(Activity activity){
        activityStack.add(activity);
    }

    /**
     * 移除指定的activity
     * @param activity
     */
    public void removeActivity(Activity activity){
        for (int i = activityStack.size() - 1; i >= 0; i--) {
            Activity activity1 = activityStack.get(i);
            if (activity.getClass().equals(activity1.getClass())) {
                activity1.finish();
                activityStack.remove(activity1);
                break;
            }
        }
    }

    /**
     * 移除当前的activity
     */
    public void removeCurrent(){
        Activity lastElement = activityStack.lastElement();
        lastElement.finish();
        activityStack.remove(lastElement);
    }

    /**
     * 移除所有的activity
     */
    public void removeAll(){
        for (int i = activityStack.size() - 1; i >= 0; i--) {
            Activity activity = activityStack.get(i);
            activity.finish();
            activityStack.remove(activity);
        }
    }
    public int getSize(){
        return activityStack.size();
    }
}
